﻿using UnityEngine;
using System.Collections;
using UnityEngineInternal;

public class SoundController : MonoBehaviour
{
    static string SoundPath = "Sound/UI/";
    static string BGMSoundPath = "Sound/BGM/";

    static int AoudioBufferNum = 10;

    static SoundController s_Sc = null;
    static public SoundController SC
    {
        get
        {
            if (s_Sc == null)
                Create();
            return s_Sc;
        }
        set { }
    }
    GameObject[] soundObject;
    GameObject BGMObject;

    AudioSource[] globalSoundSource;
    AudioSource globalSoundSource2;
    AudioSource globalBGMSource;

    static float SeVolume = 1.0f;

    public static void Create()
    {
        if (s_Sc == null)
        {
            GameObject SoundController = new GameObject("SoundController");
            s_Sc = SoundController.AddComponent("SoundController") as SoundController;

            s_Sc.globalSoundSource = new AudioSource[AoudioBufferNum];
            s_Sc.soundObject = new GameObject[AoudioBufferNum];

            
            GameObject.DontDestroyOnLoad(SoundController);

            for (int i = 0; i < AoudioBufferNum; i++)
            {
                s_Sc.soundObject[i] = new GameObject("EffectController" + i);
                s_Sc.globalSoundSource[i] = s_Sc.soundObject[i].AddComponent("AudioSource") as AudioSource;
                s_Sc.soundObject[i].transform.parent = SoundController.transform;
            }

            s_Sc.BGMObject = new GameObject("BGMController");
            s_Sc.globalBGMSource = s_Sc.BGMObject.AddComponent("AudioSource") as AudioSource;
            s_Sc.BGMObject.transform.parent = SoundController.transform;
        }
    }

    ////저장된 세팅 내용 적용
    //void Start()
    //{
    //   // 사운드 저장 내용 읽어오기 (옵션 설정값)
    //}

    static IEnumerator PlaySoundWithDelayFunc(string soundName, float delay)
    {
        yield return new WaitForSeconds(delay);
        PlaySound(soundName);
    }


    static public void PlaySoundWithDelay(string soundName, float delay)
    {
        SC.StartCoroutine(PlaySoundWithDelayFunc(soundName, delay));
    }


    static IEnumerator PlaySoundWithDelayFunc(string soundName, float delay, bool isLoop)
    {
        yield return new WaitForSeconds(delay);
        PlaySound(soundName, isLoop);
    }


    static public void PlaySoundWithDelay(string soundName, float delay, bool isLoop)
    {
        SC.StartCoroutine(PlaySoundWithDelayFunc(soundName, delay, isLoop));
    }


	static IEnumerator PlaySoundWithDelayFunc(GameObject t_GameObject, AudioClip audioClip, float delay)
	{
		yield return new WaitForSeconds(delay);
		PlaySound(t_GameObject, audioClip);
	}


	static public void PlaySoundWithDelay(GameObject t_GameObject, AudioClip audioClip, float delay)
	{
		SC.StartCoroutine(PlaySoundWithDelayFunc(t_GameObject, audioClip, delay));
	}


    static public void PlaySound(string soundName)
    {
        PlaySound(soundName, SeVolume, false);
    }


    static public void PlaySound(string soundName, bool isLoop)
    {
        PlaySound(soundName, SeVolume, isLoop);
    }


    static public void PlaySound(string soundName, float volume, bool isLoop)
    {
        for (int i = 0; i < AoudioBufferNum; i++)
        {
            if (!IsPlaying(SC.soundObject[i]))
            {
                PlaySound(SC.soundObject[i], soundName, volume, isLoop);
                break;
            }
        }
    }


    static public void StopSound(string soundName)
    {
        for (int i = 0; i < AoudioBufferNum; i++)
        {
            if (IsPlaying(SC.soundObject[i]))
            {
                AudioSource tempSource = SC.soundObject[i].GetComponent("AudioSource") as AudioSource;

                if (tempSource.clip.name == soundName)
                    tempSource.Stop();                
            }
        }
    }

    static public void StopSound(GameObject t_GameObject)
    {
        if (t_GameObject != null)
        {
            if (IsPlaying(t_GameObject))
            {
                AudioSource tempSource = t_GameObject.GetComponent("AudioSource") as AudioSource;


                tempSource.Stop();
            }
        }
    }


    static public void PlaySound(GameObject t_GameObject, string soundName)
    {
        PlaySound(t_GameObject, soundName, SeVolume, false);
    }



    static public void PlaySound(GameObject t_GameObject, AudioClip audioClip)
    {
        PlaySound(t_GameObject, audioClip, SeVolume, false);
    }



    static public void PlaySound(GameObject t_GameObject, string soundName, float volume, bool isLoop)
    {
        if (soundName == null || soundName == "")
            return;

        AudioClip temp = null;

#if USE_ASSET_BUNDLE        
            temp = AssetBundleManager.Load((int)BundleIndex.Sound, soundName) as AudioClip;     
#else
        temp = Resources.Load(string.Format("{0}{1}", SoundPath, soundName)) as AudioClip;
#endif
        if (temp != null)
            PlaySound(t_GameObject, temp, volume, isLoop);
    }



    static public void PlaySound(GameObject t_GameObject, AudioClip audioClip, float volume, bool isLoop)
    {
        AudioSource tempSource = t_GameObject.audio;

        if (tempSource == null)
            tempSource = t_GameObject.AddComponent("AudioSource") as AudioSource;

        tempSource.clip = audioClip;
        tempSource.loop = isLoop;

        if (UserData.Instance.m_EF_Sound_On_Off == true)
            tempSource.volume = volume;
        else
            tempSource.volume = 0.0f;

        tempSource.Play();
    }


    static public void PlayBGM(string soundName)
    {
        PlayBGM(soundName, true);
    }


    static public void PlayBGM(string soundName, bool loop)
    {
        if (soundName == null || soundName == "")
            return;

        AudioClip temp = null;
#if USE_ASSET_BUNDLE
            temp = AssetBundleManager.Load((int)BundleIndex.Sound, soundName) as AudioClip;
#else
        temp = Resources.Load(string.Format("{0}{1}", BGMSoundPath, soundName)) as AudioClip;
#endif        
        
        if(temp != null)
            PlayBGM(temp, loop);
    }

    static public void PlayBGM(AudioClip audioClip, bool loop)
    {
        if (audioClip == null)
            return;

        if (SC.globalBGMSource.clip == null)    //재생중인 BGM이 없을때
        {
            SC.globalBGMSource.clip = audioClip;
            SC.globalBGMSource.loop = loop;

            if (UserData.Instance.m_BG_Sound_On_Off == false)
                SC.globalBGMSource.volume = 0.0f;
            else
                SC.globalBGMSource.volume = SeVolume;


            SC.globalBGMSource.Play();
        }
        else
        {
            if (SC.globalBGMSource.clip.name != audioClip.name)  //같은 비지엠은 연속 재생
            {
                SC.globalBGMSource.clip = audioClip;
                SC.globalBGMSource.loop = loop;

                if (UserData.Instance.m_BG_Sound_On_Off == false)
                    SC.globalBGMSource.volume = 0.0f;
                else
                    SC.globalBGMSource.volume = SeVolume;

                SC.globalBGMSource.Play();
            }
        }
    }

    static public void SetBGMVolume(float Volume_val)
    {
        SC.globalBGMSource.volume = Volume_val;
    }


    static public bool IsPlaying(GameObject t_GameObject)
    {
        AudioSource tempSource = t_GameObject.audio;

        if (tempSource == null)
            tempSource = t_GameObject.AddComponent("AudioSource") as AudioSource;

        return tempSource.isPlaying;
    }
}